<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/11/14
 * Time: 10:33
 */

namespace DuRequest\Checker\lib;


use DuRequest\Checker\lib\base\CheckerInterface;

class Checker implements CheckerInterface
{
    /**
     * @var $key string
     */
    public $key ;

    public $tpl = '/\{\$key\}/';

    /**
     * @var $error_msg string
     */
    public $error_msg;
    /**
     * @var RequestData $request_data
     */
    public $request_data;

    public function __construct( $key , $error_msg)
    {
        $this->setValidateKey( $key );
        $this->setErrorMsg( $error_msg );
    }

    public function validate()
    {
        return $this->request_data->issetKey( $this->getKey() );
    }

    public function getKey(){
        return $this->key;
    }

    /**
     * @param $quest_data RequestData
     */
    public function setRequestData($quest_data)
    {
        $this->request_data = $quest_data;
    }

    /**
     * @param string $key
     */
    public function setValidateKey($key)
    {
        $this->key = $key;
    }

    /**
     * @param $error_msg string {$key}
     */
    public function setErrorMsg( $error_msg )
    {
        $this->error_msg = $error_msg;
    }

    public function issetKey(){
        return $this->request_data->issetKey( $this->getKey() );
    }

    public function getValue(){
        return $this->request_data->getDataByKey( $this->getKey() );
    }

    /**
     * @return string|null
     */
    public function getError(){
        return preg_replace( $this->tpl , $this->getKey() , $this->error_msg);
    }

}